<template>
  <div class="preview">
    <img class="preview__bg" src="@/assets/images/result-bg.png" @load="loading.loading1 = false" />
    <img :src="profile" class="preview__profile" @load="loading.loading2 = false" />
    <p class="preview__name">{{ name }}</p>
    <p class="preview__number" style="font-size: 28px">{{ number }}</p>

    <template v-if="posterUrl">
      <img class="preview__poster" :src="posterUrl" />
      <span class="preview__recreate" @click="$router.back()">重新生成</span>
      <div class="preview__indicator"></div>
    </template>
  </div>
</template>

<script setup lang="ts">
import html2canvas from 'html2canvas'
import { showLoadingToast, closeToast } from 'vant'
import { onMounted, ref, reactive } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()
const profile = ref()
const loading = reactive({ loading1: true, loading2: true })
const posterUrl = ref()

let timer: number | undefined

/** 创建海报 */
async function createPoster() {
  await checkLoadingOver()

  document.documentElement.scrollTop = 0
  setTimeout(async () => {
    closeToast()
    const canvas = await html2canvas(document.body)
    let dataURL = canvas.toDataURL('image/png', 1)
    posterUrl.value = dataURL
  }, 1000)
}

/** 监测图片是否加载完成 */
function checkLoadingOver() {
  return new Promise((resolve) => {
    showLoadingToast({
      message: '正在生成海报中...',
      duration: 0,
      forbidClick: true,
      loadingType: 'spinner',
      teleport: 'html'
    })

    timer = setInterval(() => {
      if (loading.loading1 || loading.loading2) return
      clearInterval(timer)
      resolve('')
    }, 300)
  })
}

onMounted(createPoster)

const { url = '', name, number } = route.query
profile.value = decodeURIComponent(url as string)
</script>

<style scoped lang="less">
p {
  margin: 0;
  padding: 0;
}
.preview {
  width: 100%;
  height: auto;
  overflow: hidden;
  position: relative;
  padding: 0;
  margin: 0;
  .preview__bg {
    position: relative;
    width: 100%;
    height: auto;
    z-index: 2;
    display: block;
    border: 0;
  }
  .preview__profile {
    position: absolute;
    z-index: 1;
    width: 230px;
    top: 137px;
    height: auto;
    z-index: 1;
    left: 50%;
    margin: 0;
    transform: translateX(-50%);
    display: block;
    border: 0;
  }

  .preview__name {
    position: absolute;
    z-index: 10;
    font-size: 32px;
    font-family: Inter;
    font-weight: 700;
    color: #fee4a3;
    left: 220px;
    top: 458px;
    width: 180px;
    line-height: 39px;
  }
  .preview__number {
    position: absolute;
    left: 80px;
    top: 509px;
    font-family: Inter;
    font-style: normal;
    font-weight: 700;
    font-size: 26px;
    line-height: 31px;
    color: #fcb65f;
    z-index: 10;
    width: 60px;
    text-align: center;
  }
  .preview__poster {
    position: absolute;
    width: 100%;
    height: auto;
    left: 0;
    top: 0;
    z-index: 999;
  }

  .preview__indicator {
    width: 156px;
    height: 86px;
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: 64px;
    z-index: 9999;
    background: url(@/assets/images/ts-xz.png) no-repeat;
    background-size: 100%;
    pointer-events: none;
  }

  .preview__recreate {
    font-size: 16px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #ffffff;
    line-height: 22px;
    text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: 29px;
    display: inline-block;
    z-index: 9999;
  }
}
</style>
